package com.sunwayworld.basemodule.business.result.service.impl;

import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.support.PersistableHelper;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.result.dao.LimsRunEquipDao;
import com.sunwayworld.basemodule.business.result.bean.LimsRunEquipBean;
import com.sunwayworld.basemodule.business.result.service.LimsRunEquipService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsRunEquipServiceImpl implements LimsRunEquipService {

    @Autowired
    private LimsRunEquipDao limsRunEquipDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsRunEquipDao getDao() {
        return limsRunEquipDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsRunEquipBean limsRunEquip = jsonWrapper.parseUnique(LimsRunEquipBean.class);
        limsRunEquip.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsRunEquip);
        return limsRunEquip.getId();
    }

    @Override
    public List<LimsRunEquipBean> selectEquipInfoByRunId(MapperParameter parameter) {
        List<Map<String, Object>> maps = limsRunEquipDao.selectEquipInfoByRunId(parameter);
        return maps.isEmpty() ? CollectionUtils.emptyList() :
                maps.stream().map(e -> PersistableHelper.mapToPersistable(e, LimsRunEquipBean.class)).collect(Collectors.toList());
    }
}
